package com.yonyougov.cruxtestcase.testcase.controlrule.enumvalue.multversion;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.yonyougov.cruxtestcase.component.CommonLogin;
import com.yonyougov.cruxtestcase.component.ZWCDInvoke;
import com.yonyougov.cruxtestcase.dto.BaseDto;
import com.yonyougov.cruxtestcase.dto.BaseResponseDto;
import com.yonyougov.cruxtestcase.dto.BaseTestReqDto;
import com.yonyougov.cruxtestcase.dto.EnvQueryReqDto;
import org.apache.commons.lang3.StringUtils;
import org.springframework.http.*;
import org.springframework.stereotype.Component;
import org.springframework.web.client.RestTemplate;

@Component
public class EnumCaseService {
    private final ZWCDInvoke zwcdInvoke;
    private final CommonLogin commonLogin;
    private final RestTemplate restTemplate;
    private final ObjectMapper objectMapper;
    //码值多版本不敏感查询 查不敏感 api
    private final String findEnumValueMuitiNosensitiveUrl = "/crux-basicdata/api/enumvalue/?sort=code,asc&regionId=-1&type.id=3281109494080471062&type.code=CS01010&dataEnabled=all&filterDisable=false&page=0&size=10&fetchProperties=*,region[id,lastModifiedVersion,code,name,type],parent[id,lastModifiedVersion,code,name,type]";
    //查询全部历史版本
    private final String findEnumValueMuitiAllUrl = "/crux-basicdata/api/enumvalue?disableMultiVersion=true&sort=code,asc&regionId=-1&type.id=911&type.code=CS01007&dataEnabled=all&filterDisable=false&page=0&size=99&fetchProperties=*,region[id,lastModifiedVersion,code,name,type],parent[id,lastModifiedVersion,code,name,type]";
    //码值多版本敏感查询 查敏感 api
    private final String findEnumValueMuitiSensitiveUrl = "/crux-basicdata/api/enumvalue?versionSensitive=true&sort=code,asc&regionId=-1&type.id=911&type.code=CS01007&dataEnabled=all&filterDisable=false&page=0&size=99&fetchProperties=*,region[id,lastModifiedVersion,code,name,type],parent[id,lastModifiedVersion,code,name,type]";
    //码值多版本 新增
    private final String addEnumValueMuitiUrl = "/crux-basicdata/api/enumvalue?fetchProperties=*,region[id,lastModifiedVersion,code,name,type],parent[id,lastModifiedVersion,code,name,type]";
    //码值多版本 修改
    private final String updateEnumValueMuitiUrl = "/crux-basicdata/api/enumvalue?fetchProperties=*,region[id,lastModifiedVersion,code,name,type],parent[id,lastModifiedVersion,code,name,type]";
    //码值多版本 变更
    private final String modifyEnumValueMuitiUrl = "/crux-basicdata/api/enumvalue/change?fetchProperties=*,region[id,lastModifiedVersion,code,name,type],parent[id,lastModifiedVersion,code,name,type]";

    public EnumCaseService(ZWCDInvoke zwcdInvoke, CommonLogin commonLogin, RestTemplate restTemplate, ObjectMapper objectMapper) {
        this.zwcdInvoke = zwcdInvoke;
        this.commonLogin = commonLogin;
        this.restTemplate = restTemplate;
        this.objectMapper = objectMapper;
    }

    public BaseResponseDto testcase(BaseTestReqDto baseDto) {
        BaseResponseDto baseResponseDto = new BaseResponseDto();
        String baseUrl = zwcdInvoke.getBaseUrl(baseDto.getEnvName());
        if (!StringUtils.isEmpty(baseUrl)) {
            String token = commonLogin.getToken(baseUrl, baseDto.getUser(), baseDto.getPassword());
            if (StringUtils.isNotBlank(token)) {
                baseResponseDto.setSuccess(true);
                //set header + 调用自己的api
                HttpHeaders httpHeaders = new HttpHeaders();
                httpHeaders.add("Authorization", "Bearer " + token);
                //调api
                ResponseEntity<BaseDto> dto = restTemplate.exchange(baseUrl + findEnumValueMuitiNosensitiveUrl, HttpMethod.GET, new HttpEntity<>(httpHeaders), BaseDto.class);
                if (dto.getStatusCode() == HttpStatus.OK && dto.hasBody()) {
                    try {
                        baseResponseDto.setData("myBaseDto", objectMapper.readValue(objectMapper.writeValueAsString(dto.getBody()), Object.class));
                    } catch (JsonProcessingException e) {
                        e.printStackTrace();
                    }
                }
            } else {
                baseResponseDto.setSuccess(false);
                baseResponseDto.setErrorMsg("用户名或者密码错误，获取token失败");
            }
        } else {
            baseResponseDto.setSuccess(false);
            baseResponseDto.setErrorMsg("环境不存在");
        }
        return baseResponseDto;
    }

    public BaseResponseDto testcase1(BaseTestReqDto baseDto) {
        BaseResponseDto baseResponseDto = new BaseResponseDto();
        String baseUrl = zwcdInvoke.getBaseUrl(baseDto.getEnvName());
        if (!StringUtils.isEmpty(baseUrl)) {
            String token = commonLogin.getToken(baseUrl, baseDto.getUser(), baseDto.getPassword());
            if (StringUtils.isNotBlank(token)) {
                baseResponseDto.setSuccess(true);
                //set header + 调用自己的api
                EnvQueryReqDto envQueryReqDto = new EnvQueryReqDto();
                envQueryReqDto.setName(baseDto.getEnvName());
                HttpHeaders httpHeaders = new HttpHeaders();
                httpHeaders.add("Authorization", "Bearer " + token);
                HttpEntity<EnvQueryReqDto> entity = new HttpEntity<>(envQueryReqDto, httpHeaders);
                //调api
                ResponseEntity<BaseDto> dto = restTemplate.exchange(baseUrl + findEnumValueMuitiAllUrl, HttpMethod.GET, entity, BaseDto.class);
                if (dto.getStatusCode() == HttpStatus.OK && dto.hasBody()) {
                    try {
                        baseResponseDto.setData("myBaseDto", objectMapper.readValue(objectMapper.writeValueAsString(dto.getBody()), Object.class));
                    } catch (JsonProcessingException e) {
                        e.printStackTrace();
                    }
                }
            } else {
                baseResponseDto.setSuccess(false);
                baseResponseDto.setErrorMsg("用户名或者密码错误，获取token失败");
            }
        } else {
            baseResponseDto.setSuccess(false);
            baseResponseDto.setErrorMsg("环境不存在");
        }
        return baseResponseDto;
    }

    public BaseResponseDto testcase2(BaseTestReqDto baseDto) {
        BaseResponseDto baseResponseDto = new BaseResponseDto();
        String baseUrl = zwcdInvoke.getBaseUrl(baseDto.getEnvName());
        if (!StringUtils.isEmpty(baseUrl)) {
            String token = commonLogin.getToken(baseUrl, baseDto.getUser(), baseDto.getPassword());
            if (StringUtils.isNotBlank(token)) {
                baseResponseDto.setSuccess(true);
                //set header + 调用自己的api
                EnvQueryReqDto envQueryReqDto = new EnvQueryReqDto();
                envQueryReqDto.setName(baseDto.getEnvName());
                HttpHeaders httpHeaders = new HttpHeaders();
                httpHeaders.add("Authorization", "Bearer " + token);
                HttpEntity<EnvQueryReqDto> entity = new HttpEntity<>(envQueryReqDto, httpHeaders);
                //调api
                ResponseEntity<BaseDto> dto = restTemplate.exchange(baseUrl + findEnumValueMuitiSensitiveUrl, HttpMethod.GET, entity, BaseDto.class);
                if (dto.getStatusCode() == HttpStatus.OK && dto.hasBody()) {
                    try {
                        baseResponseDto.setData("myBaseDto", objectMapper.readValue(objectMapper.writeValueAsString(dto.getBody()), Object.class));
                    } catch (JsonProcessingException e) {
                        e.printStackTrace();
                    }
                }
            } else {
                baseResponseDto.setSuccess(false);
                baseResponseDto.setErrorMsg("用户名或者密码错误，获取token失败");
            }
        } else {
            baseResponseDto.setSuccess(false);
            baseResponseDto.setErrorMsg("环境不存在");
        }
        return baseResponseDto;
    }

    public BaseResponseDto testcase3(BaseTestReqDto baseDto) {
        BaseResponseDto baseResponseDto = new BaseResponseDto();
        String baseUrl = zwcdInvoke.getBaseUrl(baseDto.getEnvName());
        if (!StringUtils.isEmpty(baseUrl)) {
            String token = commonLogin.getToken(baseUrl, baseDto.getUser(), baseDto.getPassword());
            if (StringUtils.isNotBlank(token)) {
                baseResponseDto.setSuccess(true);
                //set header + 调用自己的api
                EnumValue enumValue = null;
                try {
                    enumValue = objectMapper.readValue(objectMapper.writeValueAsString("{\n" +
                            "    \"code\":\"207\",\n" +
                            "    \"name\":\"多版本测试3\",\n" +
                            "    \"region\":\"3285292485697456694\",\n" +
                            "    \"parent\":null,\n" +
                            "    \"enabled\":false,\n" +
                            "    \"leafNode\":false,\n" +
                            "    \"type\":{\n" +
                            "        \"id\":\"911\",\n" +
                            "        \"lastModifiedVersion\":0,\n" +
                            "        \"code\":\"CS01007\",\n" +
                            "        \"name\":\"银行行别代码\",\n" +
                            "        \"key\":\"911\",\n" +
                            "        \"value\":\"911\",\n" +
                            "        \"title\":\"CS01007 银行行别代码\",\n" +
                            "        \"scopedSlots\":{\n" +
                            "            \"title\":\"title\"\n" +
                            "        },\n" +
                            "        \"children\":[\n" +
                            "\n" +
                            "        ],\n" +
                            "        \"isLeaf\":true\n" +
                            "    },\n" +
                            "    \"startDate\":\"2020-04-01\",\n" +
                            "    \"endDate\":\"2099-12-31\"\n" +
                            "}"), EnumValue.class);
                } catch (JsonProcessingException e) {
                    e.printStackTrace();
                }
                HttpHeaders httpHeaders = new HttpHeaders();
                httpHeaders.add("Authorization", "Bearer " + token);
                HttpEntity<EnvQueryReqDto> entity = new HttpEntity(enumValue, httpHeaders);
                //调api
                ResponseEntity<BaseDto> dto = restTemplate.exchange(baseUrl + addEnumValueMuitiUrl, HttpMethod.POST, entity, BaseDto.class);
                if (dto.getStatusCode() == HttpStatus.OK && dto.hasBody()) {
                    try {
                        baseResponseDto.setData("myBaseDto", objectMapper.readValue(objectMapper.writeValueAsString(dto.getBody()), Object.class));
                    } catch (JsonProcessingException e) {
                        e.printStackTrace();
                    }
                }
            } else {
                baseResponseDto.setSuccess(false);
                baseResponseDto.setErrorMsg("用户名或者密码错误，获取token失败");
            }
        } else {
            baseResponseDto.setSuccess(false);
            baseResponseDto.setErrorMsg("环境不存在");
        }
        return baseResponseDto;
    }

    public BaseResponseDto testcase4(BaseTestReqDto baseDto) {
        BaseResponseDto baseResponseDto = new BaseResponseDto();
        String baseUrl = zwcdInvoke.getBaseUrl(baseDto.getEnvName());
        if (!StringUtils.isEmpty(baseUrl)) {
            String token = commonLogin.getToken(baseUrl, baseDto.getUser(), baseDto.getPassword());
            if (StringUtils.isNotBlank(token)) {
                baseResponseDto.setSuccess(true);
                //set header + 调用自己的api
                EnumValue enumValue = null;
                try {
                    enumValue = objectMapper.readValue(objectMapper.writeValueAsString("{\n" +
                            "    \"code\":\"207\",\n" +
                            "    \"name\":\"多版本测试3\",\n" +
                            "    \"region\":\"3285292485697456694\",\n" +
                            "    \"parent\":null,\n" +
                            "    \"enabled\":false,\n" +
                            "    \"leafNode\":false,\n" +
                            "    \"type\":{\n" +
                            "        \"id\":\"911\",\n" +
                            "        \"lastModifiedVersion\":0,\n" +
                            "        \"code\":\"CS01007\",\n" +
                            "        \"name\":\"银行行别代码\",\n" +
                            "        \"key\":\"911\",\n" +
                            "        \"value\":\"911\",\n" +
                            "        \"title\":\"CS01007 银行行别代码\",\n" +
                            "        \"scopedSlots\":{\n" +
                            "            \"title\":\"title\"\n" +
                            "        },\n" +
                            "        \"children\":[\n" +
                            "\n" +
                            "        ],\n" +
                            "        \"isLeaf\":true\n" +
                            "    },\n" +
                            "    \"startDate\":\"2020-04-01\",\n" +
                            "    \"endDate\":\"2099-12-31\"\n" +
                            "}"), EnumValue.class);
                } catch (JsonProcessingException e) {
                    e.printStackTrace();
                }
                HttpHeaders httpHeaders = new HttpHeaders();
                httpHeaders.add("Authorization", "Bearer " + token);
                HttpEntity<EnvQueryReqDto> entity = new HttpEntity(enumValue, httpHeaders);
                //调api
                ResponseEntity<BaseDto> dto = restTemplate.exchange(baseUrl + updateEnumValueMuitiUrl, HttpMethod.POST, entity, BaseDto.class);
                if (dto.getStatusCode() == HttpStatus.OK && dto.hasBody()) {
                    try {
                        baseResponseDto.setData("myBaseDto", objectMapper.readValue(objectMapper.writeValueAsString(dto.getBody()), Object.class));
                    } catch (JsonProcessingException e) {
                        e.printStackTrace();
                    }
                }
            } else {
                baseResponseDto.setSuccess(false);
                baseResponseDto.setErrorMsg("用户名或者密码错误，获取token失败");
            }
        } else {
            baseResponseDto.setSuccess(false);
            baseResponseDto.setErrorMsg("环境不存在");
        }
        return baseResponseDto;
    }

    public BaseResponseDto testcase5(BaseTestReqDto baseDto) {
        BaseResponseDto baseResponseDto = new BaseResponseDto();
        String baseUrl = zwcdInvoke.getBaseUrl(baseDto.getEnvName());
        if (!StringUtils.isEmpty(baseUrl)) {
            String token = commonLogin.getToken(baseUrl, baseDto.getUser(), baseDto.getPassword());
            if (StringUtils.isNotBlank(token)) {
                baseResponseDto.setSuccess(true);
                //set header + 调用自己的api
                EnumValue enumValue = null;
                try {
                    enumValue = objectMapper.readValue(objectMapper.writeValueAsString(
                            "{\n" +
                                    "    \"code\":\"207\",\n" +
                                    "    \"name\":\"多版本测试333\",\n" +
                                    "    \"region\":\"3285292485697456694\",\n" +
                                    "    \"parent\":null,\n" +
                                    "    \"enabled\":false,\n" +
                                    "    \"leafNode\":false,\n" +
                                    "    \"type\":{\n" +
                                    "        \"id\":\"911\",\n" +
                                    "        \"lastModifiedVersion\":0,\n" +
                                    "        \"code\":\"CS01007\",\n" +
                                    "        \"name\":\"银行行别代码\",\n" +
                                    "        \"key\":\"911\",\n" +
                                    "        \"value\":\"911\",\n" +
                                    "        \"title\":\"CS01007 银行行别代码\",\n" +
                                    "        \"scopedSlots\":{\n" +
                                    "            \"title\":\"title\"\n" +
                                    "        },\n" +
                                    "        \"children\":[\n" +
                                    "\n" +
                                    "        ],\n" +
                                    "        \"isLeaf\":true\n" +
                                    "    },\n" +
                                    "    \"startDate\":\"2020-04-01\",\n" +
                                    "    \"endDate\":\"2099-12-31\"\n" +
                                    "}"), EnumValue.class);
                } catch (JsonProcessingException e) {
                    e.printStackTrace();
                }
                HttpHeaders httpHeaders = new HttpHeaders();
                httpHeaders.add("Authorization", "Bearer " + token);
                HttpEntity<EnvQueryReqDto> entity = new HttpEntity(enumValue, httpHeaders);
                //调api
                ResponseEntity<BaseDto> dto = restTemplate.exchange(baseUrl + modifyEnumValueMuitiUrl, HttpMethod.POST, entity, BaseDto.class);
                if (dto.getStatusCode() == HttpStatus.OK && dto.hasBody()) {
                    try {
                        baseResponseDto.setData("myBaseDto", objectMapper.readValue(objectMapper.writeValueAsString(dto.getBody()), Object.class));
                    } catch (JsonProcessingException e) {
                        e.printStackTrace();
                    }
                }
            } else {
                baseResponseDto.setSuccess(false);
                baseResponseDto.setErrorMsg("用户名或者密码错误，获取token失败");
            }
        } else {
            baseResponseDto.setSuccess(false);
            baseResponseDto.setErrorMsg("环境不存在");
        }
        return baseResponseDto;
    }
}
